#!/usr/bin/env python

from AutoD3PDMakerUtility import AutoD3PDMakerError

class AutoD3PDMakerTask(dict):
    '''
    A task object
    '''

    status_map = {'new'       : 'new',
                  'running'   : 'running',
                  'pause'     : 'pause',
                  'failed'    : 'failed',
                  'completed' : 'completed',
                  'finishing' : 'finishing',
                  'finished'  : 'finished',
                  'unfinished': 'unfinished',
                  'unknown'   : 'unknown'}

    def __init__(self, taskDefinition):

        if type(taskDefinition) == dict:
            dict.__init__(self, taskDefinition)
        else:
            dict.__init__(self, eval(taskDefinition))

        self.__lockedAttrs = ['id','rundef','amaconfig','input']

    def __setattr__(self, attr, value):
        if not self.__dict__.has_key('_AutoD3PDMakerTask__lockedAttrs'):
            dict.__setattr__(self, attr, value)

        if ( attr == 'status' ) and ( value not in AutoD3PDMakerTask.status_map.values() ):
            raise AutoD3PDMakerError('invalid job status assignment %s' % value)

        # raise exception if it tries to setup the locked attributes
        if attr in self.__lockedAttrs:
            if self.has_key(attr):
                raise AutoD3PDMakerError('cannot override task attribute %s:%s' % (attr,value))

        if self.__dict__.has_key(attr):
            return dict.__setattr__(self, attr, value)
        else:
            return self.__setitem__(attr, value)

    def __getattr__(self, attr):
        try:
            return self.__getitem__(attr)
        except KeyError:
            raise AttributeError(attr)

    def __str__(self):
        return repr(self)

    def __eq__(self, other):
        """
        identify the task use the internal UUID.
        """

        if self.id == other.id:
            return True
        else:
            return False